package com.atguigu.sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;


public class BubbleSort {

	public static void main(String[] args) {

		int[] arr = new int[]  { 4, 5, 6, 7, 3, 1, 2, 9};
		
		Date data1 = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date1Str = simpleDateFormat.format(data1);
		System.out.println("开始时间=" + date1Str);
		
	
		bubbleSort(arr);
		
		Date data2 = new Date();
		String date2Str = simpleDateFormat.format(data2);
		System.out.println("结束时间=" + date2Str);
		
		
		
		
	}
	
	
	public static void bubbleSort(int[] arr) {
	
		int temp = 0; 
		boolean flag = false; 
		for (int i = 0; i < arr.length - 1; i++) {

			for (int j = 0; j < arr.length - 1 - i; j++) {
				
				if (arr[j] > arr[j + 1]) {
					flag = true;
					temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = temp;
				}
			}
		
			System.out.println(i);
			System.out.println(Arrays.toString(arr));
           // 当最后一轮不需要交换的时候，跳出循环
			if (!flag) { 
				break;
			} else {
				flag = false; 
			}
		}
	}

}
